<?php

class Catnews extends AppModel {

    var $alias = 'Catnews';
    var $name = 'Catnews';
    var $useTable = 'news_categories';
    var $hasMany = array(
        'News' => array(
            'className' => 'News',
            'foreignKey' => 'cat_id',
            'dependent' => true,
        )
    );

    function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
        $parameters = compact('conditions');
        $this->recursive = $recursive;
        $count = $this->find('count', array_merge($parameters, $extra));
        if (isset($extra['group'])) {
            $count = $this->getAffectedRows();
        }
        return $count;
    }

    function getListAll() {
        $data = $this->find('list', array(
                    'fields' => 'Catnews.id, Catnews.title',
                ));
        return $data;
    }

    function writeCatNewsMenu() {
        $data = $this->find('all', array(
                    'conditions' => array('Catnews.published' => '1'),
                    'fields' => array('Catnews.id', 'Catnews.alias', 'Catnews.title'),
                    'order' => 'Catnews.id DESC'
         ));

        $html = '<ul class="submenu">' . "\n";
        foreach ($data AS $row) {
            $html .= '<li><a href="/tin-tuc/danh-muc/' . $row['Catnews']['id'] . '-' . $row['Catnews']['alias'] . '.html">' . $row['Catnews']['title'] . '</a></li>';
        }
        $html .= '</ul>';
        $resourse = fopen(CAKE_CORE_INCLUDE_PATH . DS . 'app' . DS . 'views' . DS . 'elements' . DS . 'catnews_menu.ctp', 'w');
        fwrite($resourse, $html);
    }

}

?>
